package com.edu.cs576.playerWithHyperLink;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;


public class HyperLinkPlayer {
	
	PlayerPanel playerPanel;
	private static boolean isPause = false;
	private static boolean isStop = false;
	private static boolean isPlayAfterStop = false;
	
	public HyperLinkPlayer() 
	{		
		
	}
	
	public boolean getisPlayAfterStop()
	{
		return isPlayAfterStop;
	}
	
	public void setisPlayAfterStop()
	{
		isPlayAfterStop = true;
	}
	
	public void setNotPlayAfterStop()
	{
		isPlayAfterStop = false;
	}
	
	public boolean getIsStop()
	{
		return isStop;
	}
	
	public void setIsStop()
	{
		isStop = true;
	}
	
	public void setNotStop()
	{
		isStop = false;
	}
	
	public void setIsPause()
	{
		isPause = true;
	}
	
	public void setNotPause()
	{
		isPause = false;
	}
	
	public boolean getisPause()
	{
		return isPause;
	}
	
	public void playVideo()
	{

		String fileName = "H:\\Data for 576 Final Project\\vdo5.rgb";
	   	int width = 352;
		int height = 288;
		double frameRate = (1.0 / Integer.parseInt("30")) * 1000 ;
		
		
		//System.out.println(frameRate);
		
	    BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
	    
	    // Use a label to display the image
 
	    /*
	    JFrame frame = new JFrame();	    
	    JLabel label = new JLabel(new ImageIcon(img));
	    frame.getContentPane().add(label, BorderLayout.CENTER);	    
	    frame.pack();
	    frame.setVisible(true);
	    */
	    

	    JLabel label = new JLabel(new ImageIcon(img));
	    JFrame NewFrame = new JFrame("HyperLink Player");
	    playerPanel = new PlayerPanel();
	    NewFrame.setContentPane(playerPanel.CreatLayOut(label));
	    NewFrame.setSize(800, 600);
	    NewFrame.setVisible(true);
	    
	    

	    try {
		    File file = new File(fileName);
		    InputStream is = new FileInputStream(file);

		    long len = file.length();
		    byte[] bytes = new byte[(int)len];
		    
		    int offset = 0;
	        int numRead = 0;
	    	int currentFrameNumber = 0;


	       while(true)
	       {       
	       
	       numRead = is.read(bytes, 0, width*height*3); 

	        /*
	        while (offset < bytes.length && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
	            offset += numRead;
	        }*/
	    	int ind = 0;
			for(int y = 0; y < height; y++)
			{
		
				for(int x = 0; x < width; x++)
				{
			 
					byte a = 0;
					byte r = bytes[ind];
					byte g = bytes[ind+height*width];
					byte b = bytes[ind+height*width*2]; 
					
					int pix = 0xff000000 | ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff);
					//int pix = ((a << 24) + (r << 16) + (g << 8) + b);
					img.setRGB(x,y,pix);
					ind++;
				}
			}
			
			while(getisPause() || getIsStop())
			{
				System.out.println("Pause");
			}

			
			
			NewFrame.repaint();
			

			try {
				Thread.sleep((long) frameRate);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			if(getisPlayAfterStop() == true )
			{
				is = new FileInputStream(file);
				setNotPlayAfterStop();
			}
			
			if(numRead == -1)
				is = new FileInputStream(file);

	  
	       
	    }
			
	    } catch (FileNotFoundException e) 
	    {
	      e.printStackTrace();
	    } catch (IOException e) 
	    {
	      e.printStackTrace();
	    }
	    
	    /*
	    // Use a label to display the image
	    JFrame frame = new JFrame();
	    JLabel label = new JLabel(new ImageIcon(img));
	    frame.getContentPane().add(label, BorderLayout.CENTER);
	    frame.pack();
	    frame.setVisible(true);
		*/
	   }
	   
	}


